package com.lyw.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

@RestController
@RequestMapping("/redis")
public class MyController {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @RequestMapping("/testString")
    public String testString() {
        redisTemplate.opsForValue().set("key1", "111");
        redisTemplate.opsForValue().set("key2", "222");
        redisTemplate.opsForValue().set("key3", "333");

        String value = redisTemplate.opsForValue().get("key1");
        System.out.println("value: " + value);

        return "success";
    }

    @RequestMapping("/testList")
    public String testList() {
        //清除之前的数据
        redisTemplate.execute((RedisCallback<Object>) connection -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForList().leftPush("list1", "1");
        redisTemplate.opsForList().leftPush("list1", "2");
        redisTemplate.opsForList().leftPush("list1", "3");

        System.out.println(redisTemplate.opsForList().rightPop("list1"));
        System.out.println(redisTemplate.opsForList().rightPop("list1"));
        System.out.println(redisTemplate.opsForList().rightPop("list1"));

        return "success";
    }

    @RequestMapping("/testSet")
    public String testSet() {
        redisTemplate.execute((RedisCallback<Object>) connection -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForSet().add("set1", "1", "2", "3");
        Set<String> sets = redisTemplate.opsForSet().members("set1");
        System.out.println(sets);

        Boolean exists = redisTemplate.opsForSet().isMember("set1", "1");
        System.out.println(exists);

        Long result = redisTemplate.opsForSet().size("set1");
        System.out.println(result);

        redisTemplate.opsForSet().remove("set1", "1");
        sets = redisTemplate.opsForSet().members("set1");
        System.out.println(sets);

        return "success";
    }

    @RequestMapping("/testHash")
    public String testHash() {
        redisTemplate.execute((RedisCallback<Object>) connection -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForHash().put("hash1", "key1", "value1");
        redisTemplate.opsForHash().put("hash1", "key2", "value2");
        redisTemplate.opsForHash().put("hash1", "key3", "value3");

        String s = (String) redisTemplate.opsForHash().get("hash1", "key1");
        System.out.println(s);

        Boolean exists = redisTemplate.opsForHash().hasKey("hash1", "key1");
        System.out.println(exists);

        Long result = redisTemplate.opsForHash().delete("hash1", "key1", "key2");
        System.out.println(result);

        Long size = redisTemplate.opsForHash().size("hash1");
        System.out.println(size);

        return "success";
    }

    @RequestMapping("/testZSet")
    public String testZSet() {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForZSet().add("zset1", "1", 1);
        redisTemplate.opsForZSet().add("zset1", "2", 2);
        redisTemplate.opsForZSet().add("zset1", "3", 3);

        Set<String> sets = redisTemplate.opsForZSet().range("zset1", 0, -1);
        System.out.println(sets);

        Set<ZSetOperations.TypedTuple<String>> memberWithScores = redisTemplate.opsForZSet().rangeWithScores("zset1", 0, -1);
        System.out.println(memberWithScores);

        redisTemplate.opsForZSet().remove("zset1", "1");

        sets = redisTemplate.opsForZSet().range("zset1", 0, -1);
        System.out.println(sets);

        Long index = redisTemplate.opsForZSet().rank("zset1", "2");
        System.out.println(index);

        return "success";
    }
}
